package com.xdtech.trans.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.trans.dao.TransWebSiteDao;
import com.xdtech.trans.model.TransWebSite;
import com.xdtech.trans.service.TransWebSiteService;
import com.xdtech.trans.vo.TransWebSiteItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-05-26 21:12:50
 * @since 1.0
 * @see
 */
@Service
public class TransWebSiteServiceImpl implements TransWebSiteService {
	private Log log = LogFactory.getLog(TransWebSiteServiceImpl.class);
	@Autowired
	private TransWebSiteDao transWebSiteDao;
	@Autowired
	private BaseDao baseDao;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-26 21:12:50
	 * @modified by
	 * @param entity
	 */
	public void save(TransWebSite entity) {
		transWebSiteDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-26 21:12:50
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<TransWebSite> entities) {
		transWebSiteDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-26 21:12:50
	 * @modified by
	 * @param entity
	 */
	public void delete(TransWebSite entity) {
		transWebSiteDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-26 21:12:50
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		transWebSiteDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-26 21:12:50
	 * @modified by
	 * @param id
	 * @return
	 */
	public TransWebSite get(Long id) {
		return transWebSiteDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-26 21:12:50
	 * @modified by
	 * @return
	 */
	public List<TransWebSite> getAll() {
		return transWebSiteDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-26 21:12:50
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> transWebSites = null;
		long rows = 0;
		if (pg!=null) {
			Page<TransWebSite> page = transWebSiteDao.findPage(pg,"from TransWebSite order by createTime desc", values);
			transWebSites = BeanUtils.copyListProperties(
					TransWebSiteItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<TransWebSite> transWebSiteList = transWebSiteDao.find("from TransWebSite order by id desc", values);
			transWebSites = BeanUtils.copyListProperties(
					TransWebSiteItem.class, transWebSiteList);
			rows = transWebSites.size();
		}
		results.put("total", rows);
		results.put("rows", transWebSites);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-26 21:12:50
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateTransWebSite(TransWebSiteItem item) {
		TransWebSite transWebSite = null;
		if (item.getId()==null) {
			transWebSite = new TransWebSite();
		}else {
			transWebSite = transWebSiteDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(transWebSite, item);
		if (StringUtils.isNotBlank(item.getParentId())) {
			TransWebSite parent = transWebSiteDao.get(Long.valueOf(item.getParentId()));
			transWebSite.setParent(parent);
		}
		transWebSiteDao.save(transWebSite);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-26 21:12:50
	 * @modified by
	 * @param transWebSiteId
	 * @return
	 */
	public TransWebSiteItem loadTransWebSiteItem(Long transWebSiteId) {
		TransWebSite transWebSite = transWebSiteDao.get(transWebSiteId);
		return TransWebSiteItem.createItem(transWebSite);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-26 21:12:50
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteTransWebSiteInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-26 21:12:50
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteTransWebSiteInfo(List<Long> transWebSiteIds) {
		for (Long id : transWebSiteIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-26 21:12:50
	 * @modified by
	 * @return
	 */
	@Override
	public List<TransWebSiteItem> loadItems() {
		List<TransWebSite> transWebSites = getAll();
		return TransWebSiteItem.createItems(transWebSites);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-6-9下午10:57:55
	 * @modified by
	 * @param id
	 * @return
	 */
	@Override
	public TransWebSite getPersonWebsite(Long personId) {
		TransWebSite transWebSite = transWebSiteDao.findUnique("from TransWebSite tw where tw=(select webSite from Person where id=?)", personId);
		return transWebSite;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-7-4下午11:45:04
	 * @modified by
	 * @return
	 */
	@Override
	public List<String> loadWebsiteNames() {
		List<String> names = new ArrayList<String>();
		List<TransWebSite> transWebSites = getAll();
		for (TransWebSite transWebSite : transWebSites) {
			names.add(transWebSite.getName());
		}
		return names;
	}
	/* (非 Javadoc)
	 * getWebsiteByName
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param startWebsite
	 * @return
	 * @see com.xdtech.trans.service.TransWebSiteService#getWebsiteByName(java.lang.String)
	 */
	@Override
	public TransWebSite getWebsiteByName(String startWebsite)
	{
		TransWebSite transWebSite = transWebSiteDao.findUnique("from TransWebSite where name=?", startWebsite);
		return transWebSite;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-7-10下午10:33:40
	 * @modified by
	 * @param id
	 * @return
	 */
	@Override
	public String getPersonSubWebsiteIdsById(Long id) {
		TransWebSite transWebSite = transWebSiteDao.get(id);
		StringBuffer sb = new StringBuffer();
		return getContainSubWebsiteIds(transWebSite,sb);
	}
	/**
	 * 
	 * @author max.zheng
	 * @create 2015-7-10下午10:37:58
	 * @modified by
	 * @param transWebSite
	 * @param sb
	 * @return
	 */
	private String getContainSubWebsiteIds(TransWebSite transWebSite,
			StringBuffer sb) {
		if (transWebSite!=null) {
			
			sb.append(transWebSite.getId().toString()+",");
			
			List<TransWebSite> transWebSites = transWebSite.getChildren();
			for (TransWebSite subWebsite : transWebSites) {
				getContainSubWebsiteIds(subWebsite, sb);
			}
			return sb.substring(0, sb.length()-1);
		}else {
			return sb.toString();
		}
	}

}
